﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Net;
using System.IO;
using System.Threading;


namespace FileDownloader
{

    public class Downloader
    {
        // The stream of data retrieved from the web server
        private Stream strResponse;
        // The stream of data that we write to the harddrive
        private Stream strLocal;
        // The request to the web server for file information
        private HttpWebRequest webRequest;
        // The response from the web server containing information about the file
        private HttpWebResponse webResponse;

        public String dlStatus;
        public virtual string Download(string toPath, string url)
        {

            if (!Directory.Exists(toPath))
            {
                DirectoryInfo di = Directory.CreateDirectory(toPath);
            }
            try
            {
                toPath = toPath + Path.GetFileName(url);
                WebClient wc = new WebClient();
                Uri uri = new Uri(url);
                wc.DownloadFile(url, toPath);
                return "downloaded success";
            }
            catch (WebException)
            {
                return "downloaded failed";
            }
        }
    }
}

